#include <stdio.h>
#include "10_lib.h"
int queueLength = 27;
int IsOver(queuep q,int length);
int main(void)
{
	queuep q = InitQueue();
	for(int i = 1;i <= queueLength;i++)
	{
		PushQueue(q,i);
	}
	nodep* m1 = InitStack();
	nodep* m5 = InitStack();
	nodep* m60 = InitStack();
	int hours = 0;
	while(1)
	{
		if(StackLength(m1) < 4)
		{
			StackPush(m1,OutQueue(q));
		}
		else if(StackLength(m5) < 11)
		{
			StackPush(m5,OutQueue(q));
			while(IsEmptyStack(m1) != 1)
			{
				PushQueue(q,StackPop(m1));
			}
		}
		else if(StackLength(m60) < 11)
		{
			StackPush(m60,OutQueue(q));
			while(IsEmptyStack(m1) != 1)
			{
				PushQueue(q,StackPop(m1));
			}
			while(IsEmptyStack(m5) != 1)
			{
				PushQueue(q,StackPop(m5));
			}
		}
		else
		{
			while(IsEmptyStack(m1) != 1)
			{
				PushQueue(q,StackPop(m1));
			}
			while(IsEmptyStack(m5) != 1)
			{
				PushQueue(q,StackPop(m5));
			}
			while(IsEmptyStack(m60) != 1)
			{
				PushQueue(q,StackPop(m60));
			}
			hours++;
			if(IsOver(q,queueLength) == 1)
			{
				break;
			}
		}
	}
	printf("%i\n",hours * 12);
	PrintQueue(q);
	return 0;
}
int IsOver(queuep q,int length)
{	
	nodep n = q->front;
	for(int i = 1;i <= length;i++)
	{
		if(n == NULL)
		{
			return 0;
		}
		if(i != n->data)
		{
			return 0;
		}
		n = n->next;
	}
	return 1;
}
